﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Omnia.DataModels;
using System.Windows.Input;
using Omnia.Library;
using System.Windows;
using Omnia.Library.Extensions;

namespace Omnia.ViewModels
{
    public class HomeViewModel : MainViewModel
    {
        public HomeViewModel()
        {
            SetDefaultCategory();
        }

        #region Properties
        private bool linkToCase = false;
        public bool LinkToCase
        {
            get { return linkToCase; }
            set
            {
                linkToCase = value;
                RaisePropertyChanged(() => LinkToCase);
            }
        }

        private CaseInfo selectedCase;
        public CaseInfo SelectedCase
        {
            get { return selectedCase; }
            set
            {
                selectedCase = value;
                RaisePropertyChanged(() => SelectedCase);
            }
        }

        private TransactionInfo itemToAdd = new TransactionInfo();
        public TransactionInfo ItemToAdd
        {
            get { return itemToAdd; }
            set
            {
                itemToAdd = value;
                RaisePropertyChanged(() => ItemToAdd);
                RaisePropertyChanged(() => ShowCategories);
                RaisePropertyChanged(() => AddTransactionCommand);
            }
        }

        public ObservableCollection<CategoryInfo> ShowCategories
        {
            get
            {
                return new ObservableCollection<CategoryInfo>(CategoriesCollection.Where(c => c.Kind == ItemToAdd.Kind).OrderByDescending(c => c.Default));
            }
        }

        private CategoryInfo selectedCategory;
        public CategoryInfo SelectedCategory
        {
            get { return selectedCategory; }
            set
            {
                selectedCategory = value;
                RaisePropertyChanged(() => SelectedCategory);
                ItemToAdd.CategoryGuid = SelectedCategory.Guid;
            }
        }

        public BudgetInfo CurrentBudget
        {
            get
            {
                if (Database.CollectionOfBudgets.Count(t => t.Month == DateTime.Now.Month && t.Year == DateTime.Now.Year) == 0)
                {
                    return null;
                }
                var item = Database.CollectionOfBudgets.FirstOrDefault(t => t.Month == DateTime.Now.Month && t.Year == DateTime.Now.Year);
                item.Outlay = CurrentMonthTransactionExpense.Sum;
                return item;
            }
        }

        #endregion

        #region Method
        private void Insert(TransactionInfo entity)
        {
            if (!string.IsNullOrWhiteSpace(entity.Place) && GlobalSettings.AutoSavePlace)
            {
                if (StorePlaceCollection.Count(p => p.Place == entity.Place) == 0)
                {
                    Database.CollectionOfPlaces.InsertOnSubmit(new PlaceInfo() { Place = entity.Place });
                }
            }

            if (LinkToCase && SelectedCase != null)
            {
                entity.CaseGuid = SelectedCase.Guid;
            }
            Database.CollectionOfTransactions.InsertOnSubmit(entity);
            Save();
            RaisePropertyChanged(() => TotalAmount);
            RaisePropertyChanged(() => TotalIncome);
            RaisePropertyChanged(() => TotalExpense);
            RaisePropertyChanged(() => IncomePercent);
            RaisePropertyChanged(() => CurrentMonthTransactionExpense);
            RaisePropertyChanged(() => CurrentBudget); 
            LinkToCase = false;
        }

        public void RefreshCategory()
        {
            RaisePropertyChanged(() => ShowCategories);
        }

        public void RefreshData()
        {
            RaisePropertyChanged(() => ShowCategories);
            RaisePropertyChanged(() => TotalAmount);
            RaisePropertyChanged(() => TotalIncome);
            RaisePropertyChanged(() => TotalExpense);
            RaisePropertyChanged(() => IncomePercent);
            RaisePropertyChanged(() => CurrentMonthTransactionExpense);
        }

        private void SetDefaultCategory()
        {
            if (ShowCategories != null && ShowCategories.Count > 0)
            {
                var item = ShowCategories.FirstOrDefault(c => c.Default);
                if (item == null)
                {
                    SelectedCategory = ShowCategories[0];
                }
                else
                {
                    SelectedCategory = item;
                }

                RaisePropertyChanged(() => SelectedCategory);
            }
        }
        #endregion

        #region Command
        public ICommand AddTransactionCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate {
                        Insert(ItemToAdd);
                        ItemToAdd = new TransactionInfo();
                        SetDefaultCategory();
                        MessageBox.Show(Application.Current.FindResource<string>(ResourceKeys.IDS_NOTE_TRANS_ADD_MESSAGE), Application.Current.FindResource<string>(ResourceKeys.IDS_NOTE_TRANS_ADD_MESSAGE_TITLE), MessageBoxButton.OK);
                    }
                };
            }
        }

        public ICommand CancleAddTransactionCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        ItemToAdd = new TransactionInfo();
                        SetDefaultCategory();
                        SelectedCase = null;
                        LinkToCase = false;
                    }
                };
            }
        }
        #endregion
    }
}
